package ch.exm.storm.query.relational;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import ch.exm.storm.conditions.arguments.EvaluatedArgument;
import ch.exm.storm.query.AbstractQuery;
import ch.exm.storm.query.Query;

public abstract class AbstractRelationalQuery extends AbstractQuery implements RelationalQuery {

	protected Map<String, Class<?>> aliasMap = new HashMap<String, Class<?>>();
	
	protected List<EvaluatedArgument> nonFullyEvaluatedArguments = new ArrayList<EvaluatedArgument>();
	
	@Override
	public Query clear() {
		aliasMap.clear();
		nonFullyEvaluatedArguments.clear();
		return super.clear();
	}
	
	protected void evaluateQueryPlan() {
		queryPlan = createRelationalQueryPlan();
		conditions.evaluateRelationalCondition(this);
		
		String relationalStatement = getSelectStatement() + getFromStatement() + getWhereStatement();
		getQueryPlan().setRelationalStatement(relationalStatement);
		getQueryPlan().setPostQueryCondtion(conditions.getNonRelationalCondition());
	}

	@Override
	public RelationalQueryPlan getQueryPlan() {
		return (RelationalQueryPlan)super.getQueryPlan();
	}
	
	protected abstract RelationalQueryPlan createRelationalQueryPlan();

	protected abstract String getSelectStatement();
	protected abstract String getFromStatement();
	protected abstract String getWhereStatement();
}
